package cn.yr.blog.controller.app;

import cn.yr.blog.service.CommentService;
import cn.yr.common.core.domain.entity.SysUser;
import cn.yr.common.core.domain.entity.YrComment;
import cn.yr.common.core.manager.AuthUserManager;
import cn.yr.common.exception.ret.Ret;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author lang 1102076808@qq.com
 * @Description  评论
 * @Date 15:40 2021/5/23
 **/
@Api(tags = "app评论")
@RestController
@RequestMapping("/app/comment")
public class AppCommentController {

    @Resource
    private AuthUserManager authUserManager;

    @Autowired
    private CommentService commentService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public Ret list() {
        return Ret.success(commentService.list());
    }

    /**
     * 保存
     */
    @RequestMapping("/add")
    public Ret save(@Validated @RequestBody YrComment yrComment) {
        yrComment.setCreateTime(new Date());
        SysUser user = authUserManager.getUser().getSysUser();
        yrComment.setSender(authUserManager.uid().intValue());
        yrComment.setSenderName(user.getUserName());
        yrComment.setSenderAvatar(user.getAvatar());
        yrComment.setSenderOauth(user.getOauthType());
        commentService.save(yrComment);
        return Ret.success();
    }
}
